const {
  saveDetails,
  batchAddDetails,
  eraseDetails
} = require("../services/details.service.js");
const err = require("../constant/err.type.js");

class DetailsController {
  async saveDetail(ctx, next) {
    var res;
    try {
      const { ...details } = ctx.request.body;
      if (!details.detailId) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存节点失败", data: "" }),
          ctx
        );
        return;
      }
      res = await saveDetails(details);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存节点失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存节点时出现错误，代码为" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async createDetail(ctx, next) {
    var res;
    try {
      const { ...details } = ctx.request.body;
      res = await saveDetails(details);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "创建节点失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存节点时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async batchAddDetails(ctx,next){
    var res;
    var rs;
    try {
      const {linkId,arr} = ctx.request.body;
      if(!(arr instanceof Array) || arr.length == 0){
        ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"导入数据错误，数据不能为空",data:""}),ctx);
        return;
      }
      rs = await eraseDetails({linkId});
      res = await batchAddDetails(arr);
      if(!res){
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "导入数据失败", data: "" }),
          ctx
        );
        return;
      }else{
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "导入数据出错" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }  
  async eraseDetail(ctx, next) {
    var res;
    var { detail } = ctx.request.body;
    if (!detail.linkId && !detail.detailId) {
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: -1, msg: "必须要选择一个线路或一条详情", data: "" }),
        ctx
      );
      return;
    }
    try {
      res = await eraseDetails({detail});
      console.log(res);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "删除详情失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "删除详情时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async getDetails(ctx, next) {
    var { detailId,linkId, detailKey,detailType } = ctx.request.body;
    var whereOpt = {};
    var res;
    try {
      detailId > 0 && Object.assign(whereOpt, { detailId });
      detailKey && Object.assign(whereOpt, { detailKey });
      detailType && Object.assign(whereOpt, { detailType }); 
      linkId && Object.assign(whereOpt, { linkId }); 
      res = await getDetails(whereOpt);
      if (!res) {
        ctx.app.emit("error", err.companySearchError, ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: 0, msg: "ok", data: res }),
        ctx
      );
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", err.companySearchError, ctx);
      return;
    }
  }
}

module.exports = new DetailsController();
